package org.thanos.simulator.util;

import org.thanos.simulator.api.BACnetIpClient;
import org.thanos.simulator.page.IndexPage;

import javax.swing.*;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

public class TimerUtil {

    private static final Timer TIMER = new Timer();

    public static void run(Integer period, IndexPage indexPage) {
        TIMER.schedule(new TimerTask() {
            @Override
            public void run() {
                BACnetIpClient.randomUpdateValue();
                if (Objects.nonNull(indexPage.getCurrentTreePath())) {
                    indexPage.renderContext(indexPage.getCurrentTreePath());
                }
            }
        }, 0, period * 1000);
    }

    public static void stop() {
        TIMER.cancel();
    }
}
